<?php
namespace Module\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\JsonModel;

class ModulesUsersController extends AbstractActionController
{
	protected $modulesUsersTable;
        protected $moduleTable;
        protected $userTable;
        protected $authService;
        
        public function getAuthService() {
            if(!$this->authService)
            {
                $this->authService = $this->getServiceLocator()->get('AuthService');
            }
            return $this->authService;
        }
               
        public function getModulesUsersTable()
	{
		if(!$this->modulesUsersTable)
		{
			$this->modulesUsersTable = $this->getServiceLocator()->get('Module\Model\ModulesUsersTable');
		}
		return $this->modulesUsersTable;
	}
        
        public function getModuleTable()
	{
		if(!$this->moduleTable)
		{
			$this->moduleTable = $this->getServiceLocator()->get('Module\Model\ModuleTable');
		}
		return $this->moduleTable;
	}
        
        public function getUserTable()
	{
		if(!$this->userTable)
		{
			$this->userTable = $this->getServiceLocator()->get('User\Model\UserTable');
		}
		return $this->userTable;
	}
        
        public function indexAction()
	{
            $result = $this->getModulesUsersTable()->getAll();
            
            for($i = 0; $i < count($result); $i++)
            {
                $uid = $result[$i]['uid'];
                $mid = $result[$i]['mid'];
                
                $moduleResultSet = $this->getModuleTable()->getTableGateway()->select(array('mid' => $mid));
                $module = $moduleResultSet->current();
                
                $userResultSet = $this->getUserTable()->getTableGateway()->select(array('id' => $uid));
                $user = $userResultSet->current();
                
                $result[$i]['module_name'] = $module['name'];
                
                $result[$i]['username'] = $user->getUsername();
                
                $result[$i]['user_full_name'] =  $user->getLast_name() . ' ' . $user->getMiddle_name() . ' ' . $user->getFirst_name();
                
            }
            return array('modulesusers' => $result);
        }
        
        public function registerAction()
        {
            if($this->request->isXmlHttpRequest())
            {
                if(!$this->getAuthService()->hasIdentity()) 
                {
                    return new JsonModel(array(
                        'success' => 'false',
                        'msg' => 'You have to login first!'
                    ));
                }
                else 
                {
                    $userInfo = $this->getAuthService()->getIdentity();
                    
                    $mid = (int)$this->params()->fromPost('mid');
                    $this->getModulesUsersTable()->register(array('uid' => $userInfo->id, 'mid' => $mid));
                    
                    return new JsonModel(array(
                        'success' => 'false',
                        'msg' => 'You registerd successful!'
                    ));
                }
            }
            else
            {
                if(!$this->getAuthService()->hasIdentity()) 
                {
                    $this->redirect()->toRoute('user/process', array('controller' => 'auth', 'action' => 'login'));
                }
                else 
                {
                    $userInfo = $this->getAuthService()->getIdentity();
                    $mid = (int)$this->params()->fromQuery('id');
                    $this->getModulesUsersTable()->register(array('uid' => $userInfo->id, 'mid' => $mid));
                    $this->redirect()->toRoute('module/process', array('controller' => 'modulesusers', 'action' => 'getModules'));
                }
            }
        }
        
        public function getModulesAction()
        {
            if($this->request->isXmlHttpRequest()) //AJAX
            {
                if(!$this->getAuthService()->hasIdentity()) 
                {
                    $ret = new JsonModel(array(
                        'success' => 'false',
                        'msg' => 'User have not logged in yet. Please login to continue!'
                    ));
                    return $ret;
                }
                else 
                {
                    $userInfo = $this->getAuthService()->getIdentity();

                    $uid = (int)$userInfo->id;
                    
                    $state = 1;
                    
                    if($this->params()->fromQuery('state') !== null)
                    {
                        $state = (int)$this->params()->fromQuery('state');
                    }

                    $result = $this->getModulesUsersTable()->getModulesUser($uid, $state);

                    for($i = 0; $i < count($result); $i++)
                    {
                        $mid = $result[$i]['mid'];

                        $module = $this->getModuleTable()->getById($mid);

                        $result[$i]['key_name'] = $module['key_name'];
                        $result[$i]['name'] = $module['name'];
                        $result[$i]['description'] = $module['description'];
                        $result[$i]['group_id'] = $module['module_group_id'];
                        $result[$i]['icon'] = $module['icon'];
                    }

                    $ret = new JsonModel(array(
                        'success'=>'true',
                        'modules' => $result
                    ));
                    return $ret;
                }
            }
            else { //View normal
                
                if(!$this->getAuthService()->hasIdentity()) 
                {
                    $this->redirect()->toRoute('user/process', array('controller' => 'auth', 'action' => 'login'));
                }
                else 
                {
                    $userInfo = $this->getAuthService()->getIdentity();

                    $uid = (int)$userInfo->id;

                    $state = 1;
                    
                    if($this->params()->fromQuery('state') != null)
                    {
                        $state = (int)$this->params()->fromQuery('state');
                    }

                    $result = $this->getModulesUsersTable()->getModulesUser($uid, $state);

                    for($i = 0; $i < count($result); $i++)
                    {
                        $uid = $result[$i]['uid'];
                        $mid = $result[$i]['mid'];

                        $module = $this->getModuleTable()->getById($mid);

                        $userResultSet = $this->getUserTable()->getTableGateway()->select(array('id' => $uid));
                        $user = $userResultSet->current();

                        $result[$i]['module_name'] = $module['name'];

                        $result[$i]['username'] = $user->getUsername();

                        $result[$i]['user_full_name'] =  $user->getLast_name() . ' ' . $user->getMiddle_name() . ' ' . $user->getFirst_name();
                        
                        $result[$i]['group_id'] = $module['module_group_id'];

                    }

                    return array('modules' => $result);
                }
            }
        }
        
        public function unRegisterAction()
        {
            if($this->request->isXmlHttpRequest())
            {
                if(!$this->getAuthService()->hasIdentity()) 
                {
                    $ret = new JsonModel(array(
                        'success' => 'false',
                        'msg' => 'User have not logged in yet. Please login to continue!'
                    ));
                    return $ret;
                }
                else 
                {
                   $userInfo = $this->getAuthService()->getIdentity();
                   $mid = (int)$this->params()->fromPost('id');

                   $this->getModulesUsersTable()->unregister(array('uid' => $userInfo->id, 'mid' => $mid));

                   $ret = new JsonModel(array(
                           'success' => 'true',
                           'msg' => 'Remove successful!'
                       ));
                   return $ret;
                }
            }
            else 
            {
                if(!$this->getAuthService()->hasIdentity()) 
                {
                    $this->redirect()->toRoute('user/process', array('controller' => 'auth', 'action' => 'login'));
                }
                else {
                    $userInfo = $this->getAuthService()->getIdentity();
                    $mid = $this->params()->fromQuery('mid');

                    $this->getModulesUsersTable()->unregister(array('uid' => $userInfo->id, 'mid' => $mid));
                    $this->redirect()->toRoute('module/process', array('controller' => 'modulesusers', 'action' => 'getModules'));
                }
            }
        }
        
        public function disableAction()
        {
            if($this->request->isXmlHttpRequest())
            {
                if(!$this->getAuthService()->hasIdentity()) 
                {
                    $ret = new JsonModel(array(
                        'success' => 'false',
                        'msg' => 'User have not logged in yet. Please login to continue!'
                    ));
                    return $ret;
                }
                else 
                {
                    $userInfo = $this->getAuthService()->getIdentity();
                    $mid = (int)$this->params()->fromPost('id');
                    
                    $this->getModulesUsersTable()->changeState(array('uid' => $userInfo->id, 'mid' => $mid), 0);
                    
                    $ret = new JsonModel(array(
                        'success' => 'true',
                        'msg' => 'Disable successful!'
                    ));
                    return $ret;
                }
            }
            else
            {
                if(!$this->getAuthService()->hasIdentity()) 
                {
                    $this->redirect()->toRoute('user/process', array('controller' => 'auth', 'action' => 'login'));
                }
                else 
                {
                    $userInfo = $this->getAuthService()->getIdentity();
                    $mid = (int)$this->params()->fromQuery('id');
                    
                    $this->getModulesUsersTable()->changeState(array('uid' => $userInfo->id, 'mid' => $mid), 0);
                    $this->redirect()->toRoute('module/process', array('controller' => 'modulesusers', 'action' => 'getModules'));
                }
            }
        }
        
        public function enableAction()
        {
            if($this->request->isXmlHttpRequest())
            {
                if(!$this->getAuthService()->hasIdentity()) 
                {
                    $ret = new JsonModel(array(
                        'success' => 'false',
                        'msg' => 'User have not logged in yet. Please login to continue!'
                    ));
                    return $ret;
                }
                else 
                {
                    $userInfo = $this->getAuthService()->getIdentity();
                    $mid = (int)$this->params()->fromPost('id');
                    
                    $this->getModulesUsersTable()->changeState(array('uid' => $userInfo->id, 'mid' => $mid), 1);
                    
                    $ret = new JsonModel(array(
                        'success' => 'true',
                        'msg' => 'Enable successful!'
                    ));
                    return $ret;
                }
            }
            else
            {
                if(!$this->getAuthService()->hasIdentity()) 
                {
                    $this->redirect()->toRoute('user/process', array('controller' => 'auth', 'action' => 'login'));
                }
                else 
                {
                    $userInfo = $this->getAuthService()->getIdentity();
                    $mid = (int)$this->params()->fromQuery('id');
                    
                    $this->getModulesUsersTable()->changeState(array('uid' => $userInfo->id, 'mid' => $mid), 1);
                    $this->redirect()->toRoute('module/process', array('controller' => 'modulesusers', 'action' => 'getModules'));
                }
            }
        }
}